﻿

using System;
using System.Data;
using Model;
namespace Dal
{
	public class DbExchangeDal
	{
		public DbExchangeDal()
		{
		}
 		#region Insert
 		public static int Insert(DbExchange dbExchange)
		{	
			string sql = @"Insert Into DbExchange (
					Name,
					Symbols,
					Invocation,
					Rate
				) values('{0}','{1}',{2},{3})";
			sql = string.Format(sql,
					SqlHelper.String(dbExchange.Name),
					SqlHelper.String(dbExchange.Symbols),
					dbExchange.Invocation,
					dbExchange.Rate
				);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
		
		#region Update
 		public static int Update(DbExchange dbExchange)
		{	
			string sql = @"Update DbExchange set 

					Name= '{0}',
					Symbols= '{1}',
					Invocation= {2},
					Rate= {3}
				where

					EId= {4}";
			sql = string.Format(sql,
					SqlHelper.String(dbExchange.Name),
					SqlHelper.String(dbExchange.Symbols),
					dbExchange.Invocation,
					dbExchange.Rate,
					dbExchange.EId);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
 
 

 		#region Delete
 		public static int Delete(Int32 eId)
		{
		
			string sql = @"Delete from DbExchange where 
					EId= {0}";
			sql = string.Format(sql,
					eId
);
			return SqlHelper.ExecSql(sql);
		} 
		#endregion
 
 		#region Select
 		
 		public static DataSet Select()
		{
			string sql = "select a.* from DbExchange a "; 
			return SqlHelper.Select(sql);
		}
				
		public static DbExchange Select(Int32 eId)
		{
			DbExchange dbExchange = null;
			string sql = @"select a.* from DbExchange a  where
					a.EId= {0}
";
			sql = string.Format(sql,
					eId
);
			DataSet ds = SqlHelper.Select(sql);
			if(SqlHelper.HasRow(ds))
				dbExchange = new DbExchange(ds.Tables[0].Rows[0]);					
			return dbExchange;
		}
		
		public static DataSet Select(string where)
		{ 
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from DbExchange a  where 1=1 " + where ;
			return SqlHelper.Select(sql);
		}
		
		public static DataSet SelectForList(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;
						
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from dbExchange a where 1=1 " + where;
                
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}		
		public static DataSet Select(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;			
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;			
			string sql = "select a.* from DbExchange a  where 1=1 " + where;
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}
		#endregion
	}
}
